﻿using DoNet.Bpmn.Core.Enums;
using DoNet.Bpmn.Core.Vo;
using DoNet.Bpmn.Models;
using DoNet.Bpmn.Services;
using Microsoft.Extensions.Logging;

namespace DoNet.Bpmn.Adaptor.BpmnNode
{
    /// <summary>
    /// NodePropertyOutSideAccessAdp
    /// 外部节点访问属性适配器
    /// </summary>
    public class NodePropertyOutSideAccessAdaptor : BpmnNodeAdaptor
    {
        private readonly BpmnNodeOutSideAccessConfigService _bpmnNodeOutSideAccessConfigService;
        private readonly ILogger<NodePropertyOutSideAccessAdaptor> _logger;

        public NodePropertyOutSideAccessAdaptor(
            BpmnNodeOutSideAccessConfigService bpmnNodeOutSideAccessConfigService,
            ILogger<NodePropertyOutSideAccessAdaptor> logger)
        {
            _bpmnNodeOutSideAccessConfigService = bpmnNodeOutSideAccessConfigService;
            _logger = logger;
        }

        public override BpmnNodeVo FormatToBpmnNodeVo(BpmnNodeVo bpmnNodeVo)
        {
            var nodeOutSideAccessConf = _bpmnNodeOutSideAccessConfigService.GetByBpmnNodeId(bpmnNodeVo.Id);

            if (nodeOutSideAccessConf != null)
            {
                bpmnNodeVo.Property = new BpmnNodePropertysVo
                {
                    SignType = nodeOutSideAccessConf.SignType,
                    NodeMark = nodeOutSideAccessConf.NodeMark
                };

                bpmnNodeVo.OrderedNodeType = (int)OrderNodeTypeEnum.OUT_SIDE_NODE;
            }

            return bpmnNodeVo;
        }

        public override void EditBpmnNode(BpmnNodeVo bpmnNodeVo)
        {
            var propertysVo = bpmnNodeVo.Property ?? new BpmnNodePropertysVo();

            var nodeOutSideAccessConfig = new BpmnNodeOutSideAccessConfig
            {
                BpmnNodeId = bpmnNodeVo.Id,
                SignType = propertysVo.SignType,
                NodeMark = propertysVo.NodeMark
            };

            _bpmnNodeOutSideAccessConfigService.Insert(nodeOutSideAccessConfig);
        }

        public override void SetSupportBusinessObjects()
        {
            ((IAdaptorService)this).AddSupportBusinessObjects(BpmnNodeAdpConfigEnum.ADP_CONF_NODE_PROPERTY_OUT_SIDE_ACCESS);
        }
    }
}
